clear; clc; close all

nrows = 1500;

% Percentage of x, z missing
missingx = 10; missingz = 5;

% c:continuous, b:binary
typew = ['c' 'b'];                  dimw = length(typew);
typex = ['c' 'c' 'b' 'b'];          dimx = length(typex);
typey = ['b' 'b' 'b' 'b' 'b' 'b'];  dimy = length(typey);
typez = ['c' 'c' 'b' 'b'];          dimz = length(typez);

% relations
% w = f(.), x = f(w,x), y = f(w,x), z = f(w,y,z)
%   | w  x  y  z
% --|-----------
% w | 0  0  0  0
% x | 1  1  0  0
% y | 1  1  0  0
% z | 1  0  1  1

func = [0  0  0  0
        1  1  0  0
        1  1  0  0
        1  0  1  1];

typed = [typew typex typey typez]; dimd = length(typed);
dims = [dimw, dimx, dimy, dimz];
funcd = zeros(dimd);
for i = 1:4,
    for j = 1:4,
        if(func(i,j) == 1)
            if(i > 1)
                lowi = sum(dims(1:i-1));
            else
                lowi = 0;
            end
            if(j > 1)
                lowj = sum(dims(1:j-1));
            else
                lowj = 0;
            end
            funcd(lowi+1:sum(dims(1:i)),lowj+1:sum(dims(1:j))) = 1;
        end
    end
end
for i = 1:dimd,
    for j = 1:dimd,
        if(j >= i)
            funcd(i,j) = 0;
        end
    end
end

data = zeros(nrows,dimd);
while(sum(sum(data) == 0) > 0 || sum(sum(data) == nrows) > 0 || length(unique(sum(data))) < dimd)
    coeffs = floor(11*rand(dimd)-5);
    while(sum(sum(coeffs == 0)) ~= 0)
        inds = find(coeffs == 0);
        coeffs(inds) = floor(11*rand(1,length(inds))-5);
    end
    coeffs = coeffs.*funcd;
    intercepts = floor(11*rand(dimd,1)-5);

    data = zeros(nrows,dimd);
    if(typed(1) == 'c')
        data(:,1) =  floor(11*rand(nrows,1)-5);
    else
        data(:,1) = round(rand(nrows,1));
    end
    
    isDependent = sum(funcd,2) > 0;
    for i = 2:dimd,
        if(isDependent(i))
            col = intercepts(i) + data*coeffs(i,:)';
        else
            col = floor(11*rand(nrows,1)) - 5;
        end
        if(typed(i) == 'c')
            data(:,i) = col;
        elseif(typed(i) == 'b')
            temp = exp(col);
            data(:,i) = round(temp./(temp + 1));
        end
    end
end
Y = round(rand(nrows,1));

data = [Y ones(nrows,1) data];
data_orig = data;

nMissingx = round(missingx*nrows/100);
nMissingz = round(missingz*nrows/100);

% 50% of x and z are systematically missing
data(1:nrows/2,2+dimw+1:2+dimw+dimx) = Inf;
data(nrows/2+1:end,2+dimw+dimx+dimy+1:2+dimw+dimx+dimy+dimz) = Inf;

% nMissingx and nMissingz of x and z are missing at random
rindsx = ceil(rand(1,nMissingx)*nrows/2 + nrows/2);
cindsx = ceil(rand(1,nMissingx)*dimx + 2 + dimw);
rindsz = ceil(rand(1,nMissingz)*nrows/2);
cindsz = ceil(rand(1,nMissingz)*dimz + 2 + dimw+dimx+dimy);
rinds = [rindsx rindsz];
cinds = [cindsx cindsz];
for i = 1:length(rinds),
    data(rinds(i),cinds(i)) = Inf;
end

dlmwrite('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\coeffs.txt',coeffs,'delimiter',',');
dlmwrite('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\intercepts.txt',intercepts,'delimiter',',');
dlmwrite('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\testAim2.txt',data,'delimiter',',');
dlmwrite('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\testAim2_orig.txt',data_orig,'delimiter',',');
csv2bbr('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\testAim2.txt');
csv2bbr('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\testAim2_orig.txt');
replaceinfile('Inf','NULL','C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\testAim2.txt');
replaceinfile('Inf','NULL','C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\testAim2_bbr.txt');
delete('C:\Users\Mittal\Documents\Work\Data\Adolescent\Test\*.bak')